package com.xinqing.test.algorithm.k8s.misc;

import com.feidee.fdcommon.authorization.AuthorizationVerify;
import com.feidee.fdcommon.authorization.model.AuthInfoModel;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @Author:CHQ
 * @Date:2020/10/12 14:03
 * @Description
 */
public class DeleteModel {

    public static void main(String args[]) throws IOException {
        doDelete("","https://sml.suishoudata.com/web/ui/models/serving/1a84909180aca47a");

    }

    public static String doDelete(String data, String url) throws IOException {
        CloseableHttpClient client = null;
        HttpDeleteWithBody httpDelete = null;
        String result = null;
        try {
            //分配的AccessKeyId
            String accessKeyId = "data_team";
            //分配的AccessSecret
            String accessSecret = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ6+UYiQtt2OYfyfBF8e12kjd6nnF0Zl1s23f7l+eSl07xQxoxACSXyO3aq/tF7g0IiQZev4SF7ZA/oAOIYlYo0CAwEAAQ==";
            //调用鉴权工具包计算鉴权信息,data不存则传空
            AuthInfoModel authInfo = AuthorizationVerify.getAuthInfo(url, data, accessKeyId, accessSecret);
            //获取GMT时间+
            String date = authInfo.getDate();
            //获取加密之后的data，若传入data为空，则密文也为空
            String ciphertext = authInfo.getCiphertext();
            //获取认证信息
            String authorization = authInfo.getAuthorization();

            client = HttpClients.createDefault();
            httpDelete = new HttpDeleteWithBody(url);
            httpDelete.setHeader("Date", date);
            httpDelete.setHeader("Authorization", authorization);
            httpDelete.setHeader("AuthVersion", "2");
            httpDelete.addHeader("Content-type", "application/json; charset=utf-8");
            httpDelete.setHeader("Accept", "application/json; charset=utf-8");
            httpDelete.setEntity(new StringEntity(data));

            CloseableHttpResponse response = client.execute(httpDelete);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);

            if (200 == response.getStatusLine().getStatusCode()) {

                System.out.println("DELETE方式请求远程调用成功.msg=" + result);
            }
        } catch (Exception e) {
            System.out.println("DELETE方式请求远程调用失败,errorMsg=" + e.getMessage());
        } finally {
            client.close();
        }
        return result;

    }
}



